package com.zhongge.model.dao.impl;

import com.zhongge.model.dao.UserDao;
import com.zhongge.model.entity.User;
import com.zhongge.util.JDBCUtil;

import java.sql.*;
import java.util.Date;

/**
 * @ClassName UserDaoImpl
 * @Description TODO
 * @Author zhongge
 * @Version 1.0
 */
public class UserDaoImpl implements UserDao {
    @Override
    public int insert(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        int count = 0;
        try {
            //获取连接
            conn = JDBCUtil.getConnection();

            //写出sql
            String sql =  "INSERT INTO user(username, password, nickname, email, avatar, created_time, updated_time) " +
                    "VALUES(?, ?, ?, ?, ?, ?, ?)";

            //预编译sql代码 并将sql 的 管道运输通道打通
            pstmt = conn.prepareStatement(sql);
            //为占位符赋值
            Date now = new Date();
            //getTime()返回自1970年1月1日以来，由此 Date对象表示的00:00:00 GMT的毫秒 数 。
            pstmt.setString(1, user.getUsername());
            pstmt.setString(2, user.getPassword());
            pstmt.setString(3, user.getNickname());
            pstmt.setString(4, user.getEmail());
            pstmt.setString(5, user.getAvatar());
            /*
            Timestamp(long)
            使用毫秒时间值构造一个Timestamp对象。
            积分秒存储在基础日期值中;
            分数秒存储在Timestamp对象的nanos字段中。
            time - 从1970年1月1日00:00:00 GMT以来的毫秒。
            负数是1970年1月1日00:00:00 GMT之前的毫秒数。
             */
            pstmt.setTimestamp(6, new Timestamp(now.getTime()));
            pstmt.setTimestamp(7, new Timestamp(now.getTime()));

            //最后将sql给打过去
            count =  pstmt.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(conn, pstmt, null);
        }
        return count;
    }

    @Override
    public User findByUsername(String username) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            //获取连接
            conn = JDBCUtil.getConnection();

            //写出sql
            String sql =  "SELECT * FROM user WHERE username = ?";

            //预编译sql代码 并将sql 的 管道运输通道打通
            pstmt = conn.prepareStatement(sql);

            //对占位符赋值
            pstmt.setString(1, username);

            //最后将sql给打过去
            rs = pstmt.executeQuery();

            //处理查询结果集
            if (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setEmail(rs.getString("email"));
                user.setAvatar(rs.getString("avatar"));
                user.setCreatedTime(rs.getTimestamp("created_time"));
                user.setUpdatedTime(rs.getTimestamp("updated_time"));
                return user;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(conn, pstmt, rs);
        }
        return null;
    }

    @Override
    public User findById(Integer id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            //获取连接
            conn = JDBCUtil.getConnection();

            //写出sql
            String sql =  "SELECT * FROM user WHERE id = ?";

            //预编译sql代码 并将sql 的 管道运输通道打通
            pstmt = conn.prepareStatement(sql);

            //对占位符赋值
            pstmt.setInt(1, id);

            //最后将sql给打过去
            rs = pstmt.executeQuery();

            //处理查询结果集
            if (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setEmail(rs.getString("email"));
                user.setAvatar(rs.getString("avatar"));
                user.setCreatedTime(rs.getTimestamp("created_time"));
                user.setUpdatedTime(rs.getTimestamp("updated_time"));
                return user;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(conn, pstmt, rs);
        }
        return null;
    }

    @Override
    public int update(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = "UPDATE user SET nickname = ?, email = ?, avatar = ?, updated_time = ? WHERE id = ?";
            pstmt = conn.prepareStatement(sql);

            pstmt.setString(1, user.getNickname());
            pstmt.setString(2, user.getEmail());
            pstmt.setString(3, user.getAvatar());
            pstmt.setTimestamp(4, new Timestamp(new Date().getTime()));
            pstmt.setInt(5, user.getId());

            return pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn, pstmt, null);
        }
        return 0;
    }
}
